#include "temperatureplotsettingsdialog.h"
#include "ui_temperatureplotsettingsdialog.h"

TemperaturePlotSettingsDialog::TemperaturePlotSettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TemperaturePlotSettingsDialog)
{
    ui->setupUi(this);

    ui->spinBoxXAxisRange->setRange(5, 500);
    ui->spinBoxXAxisRange->setSingleStep(5);
    ui->spinBoxXAxisRange->setValue(100);

    ui->comboBoxSelectAxis->addItem(tr("Graph Plot 1"), 1);
    ui->comboBoxSelectAxis->addItem(tr("Graph Plot 2"), 2);
    ui->comboBoxSelectAxis->addItem(tr("Graph Plot 3"), 3);

    ui->spinBoxPlotPeriod->setRange(10, 1000);
    ui->spinBoxPlotPeriod->setSingleStep(10);
    ui->spinBoxPlotPeriod->setValue(50);

    ui->checkBoxDemoPlot->setChecked(false);
    m_currentSettings.demoChecked = false;

    connect(ui->comboBoxSelectAxis, SIGNAL(currentIndexChanged(int)),
                                           this, SLOT(slotSelectedAxis(int)));
    connect(ui->checkBoxDemoPlot, SIGNAL(toggled(bool)),
            this, SLOT(slotSetDemoPlotStatus(bool)));
    connect(ui->pushButtonSetYRange, SIGNAL(clicked(bool)),
            this, SLOT(slotSetYRange()));
    connect(ui->pushButtonApply, SIGNAL(clicked(bool)),
            this, SLOT(slotApply()));
}

TemperaturePlotSettingsDialog::~TemperaturePlotSettingsDialog()
{
    delete ui;
}

TemperaturePlotSettingsDialog::PlotSettings TemperaturePlotSettingsDialog::settings() const
{
    return m_currentSettings;
}

void TemperaturePlotSettingsDialog::updateSettings()
{
    m_currentSettings.xAxisRange = ui->spinBoxXAxisRange->value();

    m_currentSettings.temperatureMAX = ui->spinBoxYAxisRangeMAX->value();
    m_currentSettings.temperatureMIN = ui->spinBoxYAxisRangeMIN->value();

    m_currentSettings.plotPeriod = ui->spinBoxPlotPeriod->value();
}

void TemperaturePlotSettingsDialog::slotSelectedAxis(int idx)
{
    if (idx == -1)
    {
        return;
    }

    m_currentSettings.nPlot = ui->comboBoxSelectAxis->itemData(idx).toInt();
}

void TemperaturePlotSettingsDialog::slotSetDemoPlotStatus(bool status)
{
        m_currentSettings.demoChecked = status;
}

void TemperaturePlotSettingsDialog::slotSetYRange()
{
    updateSettings();
    emit signalUpdatePlotSettings(1);
}

void TemperaturePlotSettingsDialog::slotApply()
{
    updateSettings();
    hide();
    emit signalUpdatePlotSettings(0);
}
